package cn.sciento.farm.devicemanager.api.jsonrpc.impl;


import cn.sciento.farm.devicemanager.api.jsonrpc.StrategyJsonApi;
import cn.sciento.farm.devicemanager.common.constant.ResultConstant;
import cn.sciento.farm.devicemanager.common.util.DateUtil;
import cn.sciento.farm.devicemanager.common.util.ReturnUtil;
import cn.sciento.farm.devicemanager.dao.entity.DevStrategy;
import cn.sciento.farm.devicemanager.dao.entity.DevStrategyLog;
import cn.sciento.farm.devicemanager.dao.model.PageConditionQuery;
import cn.sciento.farm.devicemanager.dao.model.ResponseMessage;
import cn.sciento.farm.devicemanager.dao.model.StrategyInfo;
import cn.sciento.farm.devicemanager.service.StrategyLogService;
import cn.sciento.farm.devicemanager.service.StrategyService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@AutoJsonRpcServiceImpl
public class StrategyJsonApiImpl implements StrategyJsonApi {

    @Autowired
    StrategyService strategyService;

    @Autowired
    StrategyLogService strategyLogService;

    @Override
    public ResponseMessage create(DevStrategy strategy) {
        if(strategy.getControllerId() <=0
                || strategy.getCollectionId()<=0){
           return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }else {
            strategy.setCreateTime(System.currentTimeMillis());
            strategy.setUpdateTime(System.currentTimeMillis());
            int count = strategyService.insert(strategy);
            return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,count);
        }
    }

    @Override
    public ResponseMessage update(DevStrategy strategy) {
        if (strategy.getId()<=0){
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }else {
            DevStrategy devStrategy = new DevStrategy();
            devStrategy.setId(strategy.getId());
            devStrategy.setDuration(strategy.getDuration());
            devStrategy.setBeginTime(strategy.getBeginTime());
            devStrategy.setEndTime(strategy.getEndTime());
            devStrategy.setType(strategy.getType());
            devStrategy.setStatus(strategy.getStatus());
            devStrategy.setThreshold(strategy.getThreshold());
            int count = strategyService.updateById(devStrategy);
            return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,count);
        }
    }

    @Override
    public ResponseMessage delete(Long id) {
        int count = strategyService.deleteById(id);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,count);
    }

    @Override
    public ResponseMessage list(Long controllerId) {
        List<StrategyInfo> devStrategyList = strategyService.getByControllerId(controllerId);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,devStrategyList);
    }

    @Override
    public ResponseMessage get(Long id) {
        DevStrategy devStrategy = strategyService.getById(id);
        if(devStrategy == null){
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }else {
            return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,devStrategy);
        }
    }

    @Override
    public ResponseMessage getStrategyLog(PageConditionQuery data) {
        if (data == null || data.getAreaId() == null
                || data.getAreaId() < 0
                || data.getControllerId() < 0
                || data.getCurrentPage() < 1
                || data.getPageSize() < 1
        ){
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        if (data.getCurrentPage() == null ){
            data.setCurrentPage(1);
        }
        if (data.getPageSize()== null){
            data.setPageSize(10);
        }
        if (data.getBeginTime() == null){
            Date date = DateUtil.getTodayZeroHour();
            data.setBeginTime(date.getTime());
        }
        if ( data.getEndTime() == null) {
            Date date = DateUtil.getTodayLastHour();
            data.setEndTime(date.getTime());
        }
        IPage<DevStrategyLog> devStrategyLogs = strategyLogService.getStrategyLog(data);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,devStrategyLogs);
    }
}
